package wqh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import wqh.domain.DtAlarmEvent;
import wqh.domain.DtAlarmEventConfirm;
import wqh.global.GlobalSession;
import wqh.pojo.MapRet;
import wqh.service.DtAlarmEventConfirmService;
import wqh.service.DtAlarmEventService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by cfs on 2016/12/19.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/dt_alarm_event_confirm")
public class DtAlarmEventConfirmController {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(DtAlarmEventConfirmService.class);

    @Autowired
    private DtAlarmEventService dtAlarmEventService;

    @Autowired
    private  DtAlarmEventConfirmService dtAlarmEventConfirmService;

    @RequestMapping(value = "/getDtData",method = RequestMethod.GET)
    public Object getDtData(@RequestParam(value = "draw",required = true) int draw,
                          @RequestParam(value = "start",required = true) int start,
                          @RequestParam(value = "length",required = true) int length
//                          @RequestParam(value = "order[0][column]",required = true) int column,
//                          @RequestParam(value = "order[0][dir]",required = true) String order,
//                          @RequestParam(value = "search[value]",required = true) String search
    )
    {
        try {
            PageHelper.startPage(start / length + 1, length);
            List<DtAlarmEventConfirm> dtAlarmEventConfirmList = dtAlarmEventConfirmService.findAllRecord();
            Integer count = dtAlarmEventConfirmService.selectCount();
            @SuppressWarnings("unchecked")
            PageInfo page = new PageInfo(dtAlarmEventConfirmList);
            return MapRet.DATATABLES(draw,count,new Long(page.getTotal()).intValue(),dtAlarmEventConfirmList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("显示报警事件异常：" + e.getMessage());
            return MapRet.SQL_ERROR(e);
        }
    }

    @RequestMapping(value = "/dtConfirmOne",method = RequestMethod.GET)
    public Object dtConfirmOne(HttpServletRequest request,@RequestParam(value = "id",required = true)int id)
    {
        try{
            DtAlarmEvent dtAlarmEvent = dtAlarmEventService.findById(id);
            if(dtAlarmEvent != null){
                String sessionID = request.getSession().getId();
                String user = GlobalSession.getUserNameBySessionID(sessionID);
                if(user == null) user = "空用户";
                DtAlarmEventConfirm dtAlarmEventConfirm = new DtAlarmEventConfirm(dtAlarmEvent,user);
                dtAlarmEventConfirmService.insertRecord(dtAlarmEventConfirm);
                dtAlarmEventService.deleteById(id);
            }
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("确认报警事件错误:" + e.getMessage());
            return MapRet.SQL_ERROR();
        }
    }
}
